package cn.seapon.talker;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import cn.seapon.talker.adapters.MainFragmentAdapter;
import cn.seapon.talker.bean.Account;
import cn.seapon.talker.common.MessageGenre;
import cn.seapon.talker.common.TMessage;
import cn.seapon.talker.edit.EditEmailActivity;
import cn.seapon.talker.edit.EditPasswdActivity;
import cn.seapon.talker.edit.EditPhoneActivity;
import cn.seapon.talker.edit.EditSignatureActivity;
import cn.seapon.talker.edit.NickAvatarEditActivity;
import cn.seapon.talker.fragments.FriendListFragment;
import cn.seapon.talker.fragments.HomeFragment;
import cn.seapon.talker.fragments.MessageFragment;
import cn.seapon.talker.fragments.TalkerBasedFragment;
import cn.seapon.talker.infocarrier.SharedItems;
import cn.seapon.talker.service.MessageReceptionService;

import com.viewpagerindicator.PageIndicator;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;
import com.viewpagerindicator.TitlePageIndicator.OnCenterItemClickListener;

public class MainActivity extends FragmentActivity implements
		OnCenterItemClickListener {
	private static final String TAG = "主窗口";
	private static final Random RANDOM = new Random();

	MainFragmentAdapter mAdapter;
	ViewPager mPager;
	PageIndicator mIndicator;
	public ArrayList<TalkerBasedFragment> fragmentList = new ArrayList<TalkerBasedFragment>();
	private MessageReceptionBroadcastReceiver receiver;

	private Intent messageServiceIntent;
	private UserBroadcastReceiver userInfochangeReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// ----------设置fragment----------//
		MessageFragment testFragment2 = new MessageFragment(this,
				"MessageFragment");
		fragmentList.add(testFragment2);
		FriendListFragment friendListFragment = new FriendListFragment(this,
				"FriendListFragment");
		fragmentList.add(friendListFragment);
		HomeFragment settingFragment = new HomeFragment(this, "SettingFragment");
		fragmentList.add(settingFragment);
		// ----------------------------------//

		mAdapter = new MainFragmentAdapter(getSupportFragmentManager(),fragmentList);

		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
		indicator.setViewPager(mPager);
		indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
		indicator.setOnCenterItemClickListener(this);
		mIndicator = indicator;

		messageServiceIntent = new Intent(MainActivity.this,
				MessageReceptionService.class);

		// 创建广播接受者
		// receiver = new MessageReceptionBroadcastReceiver(this);
		userInfochangeReceiver = new UserBroadcastReceiver();
	}

	@Override
	public void onCenterItemClick(int position) {
		Toast.makeText(this, "You clicked the center title!",
				Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onStart() {
		// 注册广播接受者
		// registerReceiver(receiver, new
		// IntentFilter("cn.seapon.talker.MESSAGE_RECEIVER"));
		receiver = new MessageReceptionBroadcastReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("cn.seapon.talker.MESSAGE_RECEIVER");
		MainActivity.this.registerReceiver(receiver, filter);
		
		startService(messageServiceIntent);
		super.onStart();
	}

	@Override
	protected void onStop() {
		// unregisterReceiver(receiver);
		MainActivity.this.unregisterReceiver(receiver);
		stopService(messageServiceIntent);
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.random:
			final int page = RANDOM.nextInt(mAdapter.getCount());
			Toast.makeText(this, "Changing to page " + page, Toast.LENGTH_SHORT)
					.show();
			;
			mPager.setCurrentItem(page);
			return true;

		case R.id.add_page:

			return true;

		case R.id.remove_page:

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public class MessageReceptionBroadcastReceiver extends BroadcastReceiver {
		private static final String TAG = "||__MessageReceptionBroadcastReceiver__||";
		private Context context;

		public MessageReceptionBroadcastReceiver() {
		}

		public MessageReceptionBroadcastReceiver(Context context) {
			this.context = context;
		}

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			String json = bundle.getString("MessageReceived");

		}

	}

	public void emailEdit(View view) {
		Intent intent = new Intent(this, EditEmailActivity.class);
		startActivity(intent);
	}

	public void phoneEdit(View view) {
		Intent intent = new Intent(this, EditPhoneActivity.class);
		startActivity(intent);
	}

	public void signatureEdit(View view) {
		Intent intent = new Intent(this, EditSignatureActivity.class);
		startActivity(intent);
	}

	public void nickAvatarEdit(View view) {
		Intent intent = new Intent(this, NickAvatarEditActivity.class);
		startActivity(intent);
	}

	public void passwdEdit(View view) {
		Intent intent = new Intent(this, EditPasswdActivity.class);
		startActivity(intent);
	}

	public void friendSearch(View view) {
		Intent intent = new Intent(this, SearchFriendActivity.class);
		startActivity(intent);
	}

	public void logout(View view) {
		// 取消当前用户的自动登录
		Account account = new Account();
		account.setName(SharedItems.userInformation.getAccount());
		account.setIs_default(false);
		SharedItems.accountDao.update(account);
		try {
			SharedItems.socket.close();
			// 停止服务
			stopService(messageServiceIntent);
			TMessage tMessage = new TMessage();
			tMessage.setSenderId(SharedItems.userInformation.getAccount());
			tMessage.setGenre(MessageGenre.LOGOUT_REQUEST);
			Intent intent = new Intent(MainActivity.this, LoginActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);
			finish();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public class UserBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
//			cn.seapon.talker.USER_INFO_CHANGE
		}
		
	}

}